/**
 * 
 */
package com.common.distributed.sharding.spring.config;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import com.common.distributed.sharding.spring.schema.DistributedClientSchema;

/**
 * @author liubing
 *
 */
public class DistributedClientBeanParser extends AbstractSimpleBeanDefinitionParser{
	
	public DistributedClientBeanParser(){
		
	}
	
	@Override  
    protected Class<DistributedClientSchema> getBeanClass(Element element) {  
        return DistributedClientSchema.class;  
    } 
	
	@Override
	protected void doParse(Element element, ParserContext parserContext,
			BeanDefinitionBuilder builder) {
		// TODO Auto-generated method stub
		 try {  
	            builder.addPropertyValue("map", parseMapElement(element,  
	                    parserContext, builder)); 
	            builder.addPropertyValue("type", element.getAttribute("type"));
	        } catch (Exception e) {  
	            parserContext.getReaderContext()  
	                    .error(  
	                            "class " + DistributedClientBeanParser.class.getName()  
	                                    + " can not be create", element, e);  
	        } 
	}
	
	@SuppressWarnings("all")  
    public Map parseMapElement(Element mapEle,   
            ParserContext parserContext, BeanDefinitionBuilder builder) {  
  
        List entryEles = DomUtils.getChildElementsByTagName(mapEle, "database");  
                // 关键是以下这个ManagedMap类型，充当着一个map类型的beandefinition类型的说明对象  
        ManagedMap map = new ManagedMap(entryEles.size());  
        map.setMergeEnabled(true);  
        map.setSource(parserContext.getReaderContext().extractSource(mapEle));  
  
        for (Iterator it = entryEles.iterator(); it.hasNext();) {  
            Element entryEle = (Element) it.next();  
      
            String databaseName = entryEle.getAttribute("databaseName");  
  
            map.put(databaseName, parserContext.getDelegate().parseCustomElement(  
                    entryEle, builder.getRawBeanDefinition()));  
  
        }  
  
        return map;  
    }  
}
